package com.egoo.ticket.server.controller;

import com.egoo.ticket.server.pojo.dto.WorkOrderLogDto;
import com.egoo.ticket.server.pojo.entity.WorkOrderLog;
import com.egoo.ticket.server.service.WorkOrderLogService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Donny_dong
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1/workOrderLog", produces = "application/json")
@Api(value = "工单日志管理",tags = "工单日志管理",description = "工单日志管理")
public class WorkOrderLogController {

	@Autowired
	private WorkOrderLogService workOrderLogService;

	@ApiOperation(value = "获取工单日志列表", notes = "获取工单日志列表")
	@RequestMapping(method = RequestMethod.POST, value = "/getLoglist")
	public DbResponse getLoglist(@RequestBody WorkOrderLogDto workOrderLogDto,
								 HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		workOrderLogDto.setTenantId(tenantId);
		try {
			if (workOrderLogDto.getCurrentPage() == null || workOrderLogDto.getPageSize() == null) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE,
						"currentPage和pageSize为必传参数！");
			}
			// 查询列表
			return workOrderLogService.getWorkOrderList(workOrderLogDto);
		} catch (Exception e) {
			log.info("获取工单日志列表异常：" + e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, ResponseConstant.SELECT_FAIL_MSG);
		}
	}
	@ApiOperation(value = "工单日志新增", notes = "工单日志新增")
	@RequestMapping(method = RequestMethod.POST, value = "/workOrderLogAdd")
	public DbResponse workOrderLogAdd(@RequestBody WorkOrderLog workOrderLog,
									  HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		try {
			
			// 参数校验
			DbResponse dbResponse = validateParam(workOrderLog);
			if (dbResponse != null) {
				return dbResponse;
			}
			workOrderLog.setTenantId(tenantId);
			// 日志新增
			 return  workOrderLogService.workOrderLogAdd(workOrderLog);

		} catch (Exception e) {
			log.info("工单日志新增异常：" + e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, ResponseConstant.ADD_FAIL_MSG);
		}
	}
	/**
	 * TODO 参数校验
	 * 
	 * @param workOrderLog
	 */
	private DbResponse validateParam(WorkOrderLog workOrderLog) {
		if (workOrderLog.getWorkOrderId() == null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "工单id不能为空");
		}
		
		if (workOrderLog.getLogCreaterId() == null) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "创建人不能为空");
		}

		if (StringUtils.isEmpty(workOrderLog.getLogType())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "日志类型不能为空");
		}
		
		if (StringUtils.isEmpty(workOrderLog.getContent())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "内容不能为空");
		}
		return null;
	}
}
